﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;

namespace Mahanet.Abyss.PathCalculation
{
	public class DisplayerGunFacade : IGunFacade2D
	{
		#region Fields

		private IDataDisplayer m_DataDisplayer;
		private IClock m_Clock;
		private double m_TravelSpeed = 6;
		private double m_CurrentAngle = 0;

		#endregion

		public DisplayerGunFacade(IDataDisplayer dataDisplayer, IClock clock)
		{
			this.m_DataDisplayer = dataDisplayer;
			this.m_Clock = clock;
		}

		public void Fire(Coordinate2D interceptionPoint)
		{
			m_TicksSinceFire = 0;
			this.m_Clock.Tick += m_Clock_Tick;

			this.m_DataDisplayer.AddDataPoint("InterceptionPoint", interceptionPoint.X, interceptionPoint.Y, Colors.Tan, 6);
		}

		public void ChangeGunAngle(double angle)
		{
			m_CurrentAngle = angle;

			m_Clock.Tick -= m_Clock_Tick;

			m_DataDisplayer.AddGunAngle(this.RotationCenter, this.CurrentGunDirection);
		}

		public double TravelTimeByDistance(double d)
		{
			return d / m_TravelSpeed;
		}

		public double AngularSkewByDistance(double d)
		{
			return 0;
		}

		public Coordinate2D RotationCenter { get; set; }

		public Vector2D GunZeroAngleDirection
		{
			get
			{
				return new Vector2D(-1, 0);
			}
		}



		private Vector2D CurrentGunDirection
		{
			get
			{
				return GunZeroAngleDirection.RotateBy(-m_CurrentAngle);
			}
		}
		private double m_TicksSinceFire;
		private void m_Clock_Tick()
		{
			Coordinate2D currentLocation = this.RotationCenter;
			currentLocation += this.CurrentGunDirection * m_TicksSinceFire * m_TravelSpeed;
			m_DataDisplayer.AddDataPoint("FirePath", currentLocation.X, currentLocation.Y);

			m_TicksSinceFire++;
		}
	}
}
